package utils;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.gnu.stealthp.rsslib.RSSChannel;
import org.gnu.stealthp.rsslib.RSSException;
import org.gnu.stealthp.rsslib.RSSHandler;
import org.gnu.stealthp.rsslib.RSSItem;
import org.gnu.stealthp.rsslib.RSSParser;

public class AirDate {
	
	private static String urlAirDate = "airDate.xml";
	private static DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
	
	public static Date getAirDate(String tvShow) throws ParseException{
		
		RSSHandler hand = new RSSHandler();
		//RSSParser.parseXmlFile(new URL("http://showrss.karmorra.info/feeds/424.rss"), hand, false);
		try {
			RSSParser.parseXmlFile(urlAirDate, hand, false);
		} catch (RSSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		RSSChannel ch = hand.getRSSChannel();
	
		LinkedList<RSSItem> items = hand.getRSSChannel().getItems();
		for(RSSItem item : items){
			String description = item.getDescription();
			Matcher matcher = Pattern.compile("Air date: (\\d\\d/\\d\\d/\\d\\d\\d\\d)").matcher(description);
			boolean find = matcher.find(); 
			if(find){
				String date = matcher.group(1);
				return dateFormat.parse(date);
			}
		}
		
		return null;
		
	}

}
